﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Data;

using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
using System.Text;
using weikebuluo.DAL;


public class BaseTemp
{
    private static VelocityEngine velocity = null;
    /// <summary>
    /// 获取当前应用的模板名称
    /// </summary>
    /// <returns></returns>
    public string GetCurrentTemplateName()
    {
        return "DefualtTemplate";
    }

    /// <summary>
    /// 实例化Velocity引擎并返回模板
    /// </summary>
    public string WriterTemplate(string template, IContext context = null, int expires = 60)
    {
        if (velocity == null)
            velocity = new VelocityEngine();
        velocity.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
        velocity.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.HttpContext.Current.Server.MapPath(@"\"));
        
        if (expires != 0)
        {
            velocity.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);
            velocity.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)expires);//设置被缓模板
        }
        velocity.Init();
        Template tmp = velocity.GetTemplate(template);
        StringWriter writer = new StringWriter();
        if (context == null)
            context = new VelocityContext();
        tmp.Merge(context, writer);
        return writer.ToString();
    }
}
